package com.shanks.framework.mq.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * MDC 工具类
 */
public class MDCUtil {
    private static final Logger logger = LoggerFactory.getLogger(MDCUtil.class);

    private static final String TRACE_ID = "trace_id";

    /**
     * 放入traceId
     */
    public static void putTraceId() {
        try {
            MDC.put(TRACE_ID, UUID.randomUUID().toString());
        } catch (Exception e) {
            logger.error("put trace_id fail ", e);
        }
    }

    /**
     * 放入traceId
     */
    public static void putTraceId(String traceId) {
        try {
            MDC.put(TRACE_ID, traceId);
        } catch (Exception e) {
            logger.error("put trace_id fail ", e);
        }
    }

    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }


    /**
     * 移除traceId
     */
    public static void removeTraceId() {
        try {
            MDC.remove(TRACE_ID);
        } catch (Exception e) {
            logger.error("remove trace_id fail ", e);
        }
    }
}
